Skip to content

new tevents analytics framework#1894

Merged
sawka merged 24 commits intomainfrom
sawka/analytics2
Feb 3, 2025
Merged

new tevents analytics framework#1894
sawka merged 24 commits intomainfrom
sawka/analytics2

Conversation

@sawka
Copy link
Member

@sawka sawka commented Feb 1, 2025

No description provided.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 1, 2025

Warning

Rate limit exceeded

@sawka has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 19 minutes and 26 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between fbb2e77 and bb5a3ac.

📒 Files selected for processing (8)
  • Taskfile.yml (2 hunks)
  • cmd/generatego/main-generatego.go (1 hunks)
  • frontend/app/store/wshclientapi.ts (2 hunks)
  • frontend/types/gotypes.d.ts (1 hunks)
  • pkg/wshrpc/wshclient/wshclient.go (3 hunks)
  • pkg/wshrpc/wshrpctypes.go (2 hunks)
  • pkg/wshrpc/wshserver/wshserver.go (4 hunks)
  • pkg/wshutil/wshutil.go (1 hunks)

Walkthrough

This pull request introduces significant enhancements to the telemetry system across multiple components of the application. Changes include modifications to the ROADMAP.md file to clarify targeting dates for upcoming versions, updates to environment variables in Taskfile.yml, and the introduction of new telemetry-related functions and data structures in various files.

New database migrations are included for storing telemetry events, and several functions are added for recording, validating, and sending these events. The telemetry system now captures detailed application activity, such as active minutes and foreground minutes, while also incorporating geo-location data tracking through CloudFlare headers, ensuring user privacy by not storing IP addresses. Additionally, the implementation supports batch processing of telemetry events, enhances error handling, and includes mechanisms for cleaning up old events. Overall, these changes provide a more comprehensive approach to tracking application usage and performance metrics.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (8)
pkg/telemetry/telemetrydata/telemetrydata.go (2)

16-20: Consider making ValidEventNames configurable.
If you expect new event names in the future, you might want a more flexible approach than a hard-coded map. Otherwise, you'll need code changes each time a new event type is introduced.


83-89: Time zone awareness.
ConvertToWallClockPT is explicitly converting to Pacific Time. Ensure this aligns with your requirements if your user base is global. Storing UTC and rendering times per user locale might be a more flexible approach.

pkg/wcloud/wcloud.go (1)

191-211: Iterative sending with a cap of 10 iterations.
This is a practical safeguard against infinite loops. If you foresee exceptionally large backlogs or extremely slow networks, you might consider adjusting or making it configurable.

cmd/server/main-server.go (1)

184-192: Graceful shutdown event.
Recording a shutdown event and truncating activity is a good blueprint for capturing final telemetry status. Consider increasing the 1-second context if you notice data not making it in time.

cmd/wsh/cmd/wshcmd-debug.go (1)

40-43: Execution logic is concise.
Calling SendTelemetryCommand directly is a simple approach. Consider printing a success message upon completion.

 func debugSendTelemetryRun(cmd *cobra.Command, args []string) error {
 	err := wshclient.SendTelemetryCommand(RpcClient, nil)
 	return err
+	// Optionally log a success message, e.g.:
+	// if err == nil {
+	//   fmt.Println("Telemetry has been sent successfully.")
+	// }
 }
pkg/util/utilfn/utilfn.go (1)

34-42: Consider using a more descriptive variable name.

While PTLoc works, a more descriptive name like PacificTimeLocation would improve code readability.

db/migrations-wstore/000007_events.up.sql (1)

1-8: Consider adding indexes for query optimization.

The table schema looks good, but consider adding indexes on:

  • ts for efficient time-based queries
  • uploaded for efficient batch processing

Apply this diff to add the indexes:

 CREATE TABLE db_tevent (
    uuid varchar(36) PRIMARY KEY,
    ts int NOT NULL,
    tslocal varchar(100) NOT NULL,
    event varchar(50) NOT NULL,
    props json NOT NULL,
    uploaded boolean NOT NULL DEFAULT 0
 );
+CREATE INDEX idx_tevent_ts ON db_tevent(ts);
+CREATE INDEX idx_tevent_uploaded ON db_tevent(uploaded);

Consider reducing the length of tslocal.

The length of 100 characters for tslocal seems excessive for a timestamp string.

Apply this diff to reduce the length:

 CREATE TABLE db_tevent (
    uuid varchar(36) PRIMARY KEY,
    ts int NOT NULL,
-   tslocal varchar(100) NOT NULL,
+   tslocal varchar(32) NOT NULL,
    event varchar(50) NOT NULL,
    props json NOT NULL,
    uploaded boolean NOT NULL DEFAULT 0
 );
docs/docs/telemetry.mdx (1)

99-107: Review Comment: New "Geo Data" Section in Telemetry Documentation

The added "Geo Data" section clearly explains the use of CloudFlare’s Geo-Location headers (CFCountry and CFRegionCode) and reinforces that IP addresses are not stored. One note: consider correcting the typo in CFRegionCode's description—"provence" should likely be "province" (unless intentionally phrased otherwise).

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1a70c83 and 7b0769f.

📒 Files selected for processing (23)
  • ROADMAP.md (1 hunks)
  • Taskfile.yml (2 hunks)
  • cmd/generatego/main-generatego.go (1 hunks)
  • cmd/server/main-server.go (6 hunks)
  • cmd/wsh/cmd/wshcmd-debug.go (1 hunks)
  • cmd/wsh/cmd/wshcmd-token.go (0 hunks)
  • db/migrations-wstore/000007_events.down.sql (1 hunks)
  • db/migrations-wstore/000007_events.up.sql (1 hunks)
  • docs/docs/telemetry.mdx (1 hunks)
  • emain/emain.ts (1 hunks)
  • frontend/app/store/wshclientapi.ts (2 hunks)
  • frontend/types/gotypes.d.ts (1 hunks)
  • pkg/panichandler/panichandler.go (1 hunks)
  • pkg/telemetry/telemetry.go (3 hunks)
  • pkg/telemetry/telemetrydata/telemetrydata.go (1 hunks)
  • pkg/util/dbutil/dbutil.go (9 hunks)
  • pkg/util/utilfn/streamtolines.go (1 hunks)
  • pkg/util/utilfn/utilfn.go (4 hunks)
  • pkg/wcloud/wcloud.go (3 hunks)
  • pkg/wshrpc/wshclient/wshclient.go (3 hunks)
  • pkg/wshrpc/wshrpctypes.go (2 hunks)
  • pkg/wshrpc/wshserver/wshserver.go (2 hunks)
  • pkg/wshutil/wshutil.go (1 hunks)
💤 Files with no reviewable changes (1)
  • cmd/wsh/cmd/wshcmd-token.go
✅ Files skipped from review due to trivial changes (2)
  • db/migrations-wstore/000007_events.down.sql
  • ROADMAP.md
🔇 Additional comments (45)
pkg/telemetry/telemetrydata/telemetrydata.go (5)

22-34: Struct naming is consistent and straightforward.
The TEvent struct is clear, and the separation of RawProps (for scanning) vs. Props (for direct usage) is a neat approach to avoid any scanning pitfalls. No major issues noticed.


36-47: Potential PII concerns.
Properties like country code and region code can be sensitive in some jurisdictions. Ensure you have proper user consents and that your privacy policy covers the usage of such data.


60-69: Ensure you validate newly created events.
While MakeTEvent is convenient for quickly creating an event, it might be prudent to call Validate before persisting—especially if there's any possibility of invalid input.


71-81: Properly handling map-based props.
MakeUntypedTEvent is a good utility for converting generic maps into typed structs. Do confirm that any missing fields in the map do not cause unexpected behavior downstream.


114-167: Validation logic is comprehensive.
The checks for empty or malformed fields, allowed event names, and property size are thorough. However, be aware that enforcing the event timestamp to be within ±60 seconds (when current is true) may reject valid events from slightly skewed systems.

pkg/wcloud/wcloud.go (5)

21-21: New import recognized.
The import of telemetrydata is a sensible extension for handling telemetry event data.


47-47: Endpoint naming is consistent.
TEventsUrl at /tevents is a clear and concise choice. No issues.


153-156: Well-structured input type.
TEventsInputType neatly groups ClientId and Events. Ensure ClientId remains non-sensitive.


160-189: Batch processing logic is sound.
sendTEventsBatch retrieves a batch of unuploaded events, sends them, and marks them as uploaded when successful. This ensures minimal duplication and is a robust approach.


213-232: Consolidated telemetry sending.
SendAllTelemetry cleanly invokes both sendTEvents and legacy sendTelemetry. This combined approach avoids missing data in separate flows.

cmd/server/main-server.go (3)

86-86: Renaming to startConfigWatcher suggests improved clarity.
Renaming from configWatcher to startConfigWatcher quickly conveys that it's meant to initiate a process.


117-117: Extended timeout to 10 seconds.
Increasing the telemetry send timeout can reduce failures on slower networks. If your environment can handle it, this is a sensible move.


154-169: Detailed startup TEvent.
Capturing build time, version, OS release, etc., is invaluable for diagnosing user environment issues. Ensure your privacy policy addresses any user environment data.

pkg/telemetry/telemetry.go (11)

9-10: Imports appear correct and necessary.
No issues found.

Also applies to: 14-14, 16-16, 19-19


27-27: Constant name is descriptive.
Defining ActivityEventName improves readability for referencing this specific event.


92-94: Deferred panic handling is cleanly implemented.
This approach ensures robust error logging without impacting the calling function's flow.


126-131: Straightforward property accumulation.
No issues discovered.


182-199: Asynchronous event recording looks good.
The goroutine-based approach with panic handling is consistent with the rest of the design.


200-216: Conditional routing for activity events is clear.
No issues found in handling different event types.


218-226: Old telemetry events cleanup logic is straightforward.
No concerns regarding the fixed 28-day window.


228-241: Fetching and JSON conversion of non-uploaded events works as expected.
The error path on JSON unmarshal is handled properly.


243-253: Marking telemetry events as uploaded is well-structured.
Code is self-explanatory and succinct.


133-163: Confirm rounding up to the next hour.
Rounding the timestamp backward or forward can cause data classification nuances. Verify this is intended and won’t double-count.

✅ Verification successful

Rounding logic is intentional and safeguards against double-counting.
The function explicitly rounds the current time up to the next hour with time.Now().Truncate(time.Hour).Add(time.Hour). Subsequent events within the same hour bucket reuse the same timestamp to update an existing record (via a SELECT‑then‑UPDATE strategy) instead of inserting another record, so double-counting is avoided.

  • Confirmed usage in pkg/telemetry/telemetry.go matches the intended rounding logic.
  • The update mechanism (merging activity properties) prevents duplicate hourly entries.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Look for references to updateActivityTEvent usage to confirm the rationale for rounding
rg -A 5 "updateActivityTEvent"

Length of output: 842


105-124: Verify error handling in database insertion.
Although the framework likely captures SQL errors through wstore.WithTx, confirm that tx.Exec failures are properly surfaced.

pkg/panichandler/panichandler.go (1)

33-35: Deferred wrapper for panic handling is appropriate.
This preserves clarity while ensuring the panic handler is triggered correctly.

cmd/wsh/cmd/wshcmd-debug.go (2)

27-33: New debug command is well hidden.
Providing a dedicated command for sending telemetry is a useful troubleshooting feature.


36-36: Adding the telemetry subcommand completes integration.
No issues found.

pkg/util/utilfn/streamtolines.go (1)

61-61: Verify the impact of increased buffer size.

The buffer size has been increased from 16KB to 64KB. While this may improve performance for processing large files, it also increases memory usage.

Run the following script to analyze the typical line lengths in the codebase to validate if this buffer size increase is justified:

cmd/generatego/main-generatego.go (1)

27-27: LGTM!

The addition of the telemetry data package import aligns with the PR's objective of implementing a new telemetry analytics framework.

pkg/util/dbutil/dbutil.go (2)

Line range hint 14-235: LGTM!

The replacement of interface{} with any improves type safety and readability while maintaining the same functionality.


237-264: LGTM!

The new JSON parsing functions are well-implemented with proper error handling and type safety through generics.

A few notable strengths:

  • ParseJsonMap has a forceMake parameter to control map initialization
  • ParseJsonArr uses generics for type-safe array parsing
  • Both functions handle empty input gracefully
pkg/wshutil/wshutil.go (1)

159-161: LGTM!

The wrapping of panichandler.PanicHandlerNoTelemetry in a deferred anonymous function is a good practice that ensures proper panic handling.

frontend/app/store/wshclientapi.ts (1)

255-258: LGTM!

The new command methods follow the established pattern and maintain consistency with the existing codebase.

Also applies to: 345-348

pkg/util/utilfn/utilfn.go (2)

1013-1018: LGTM!

The ConvertToWallClockPT function correctly handles timezone conversion using the initialized PTLoc.


88-104: Consider adding bounds checking for numeric conversions.

The ConvertInt function might lose precision when converting large float64 values to int64.

Run this script to check for potential precision loss in the codebase:

emain/emain.ts (1)

475-486: LGTM!

The telemetry event recording is well-structured and follows best practices:

  1. Uses fire-and-forget pattern with noresponse: true
  2. Properly categorizes the event as "app:activity"
  3. Includes relevant activity metrics as properties
pkg/wshrpc/wshclient/wshclient.go (1)

310-314: LGTM!

Both command functions follow the established pattern and maintain consistency with the existing codebase:

  1. Use the standard sendRpcRequestCallHelper for RPC calls
  2. Follow consistent error handling patterns
  3. Properly type the input parameters

Also applies to: 414-418

pkg/wshrpc/wshrpctypes.go (1)

181-181: LGTM!

The new telemetry methods are well-defined and follow the interface's conventions.

Also applies to: 185-185

pkg/wshrpc/wshserver/wshserver.go (2)

739-745: LGTM!

The RecordTEventCommand implementation correctly handles telemetry event recording and error logging.


747-754: LGTM!

The SendTelemetryCommand implementation correctly retrieves client data and sends telemetry.

frontend/types/gotypes.d.ts (3)

748-755: LGTM!

The TEvent type definition correctly matches the database schema and includes all necessary fields.


757-765: LGTM!

The TEventProps type definition provides a comprehensive structure for event properties and activity metrics.


767-779: LGTM!

The TEventUserProps type definition includes all relevant user-specific properties.

Taskfile.yml (2)

29-30: Review Comment: Validate Updated Telemetry Endpoints for electron:dev

The updated environment variables now set WCLOUD_ENDPOINT to "https://api-dev.waveterm.dev/central" and WCLOUD_WS_ENDPOINT to "wss://wsapi-dev.waveterm.dev/". Please confirm that including the /central suffix for the dev task is intentional and aligns with your new telemetry backend requirements.


42-43: Review Comment: Check Endpoint Consistency in electron:start

For the electron:start task, WCLOUD_ENDPOINT is updated to "https://api-dev.waveterm.dev" (without the /central suffix) and WCLOUD_WS_ENDPOINT is set to "wss://wsapi-dev.waveterm.dev". Ensure these differences from the electron:dev task—especially the missing /central suffix and the trailing slash discrepancy in WCLOUD_WS_ENDPOINT—are intentional and reflect the correct endpoints for production versus development.

Comment on lines +165 to +180
func TruncateActivityTEventForShutdown(ctx context.Context) error {
nowTs := time.Now()
eventTs := nowTs.Truncate(time.Hour).Add(time.Hour)
return wstore.WithTx(ctx, func(tx *wstore.TxWrap) error {
// find event that matches this timestamp with event name "app:activity"
uuidStr := tx.GetString(`SELECT uuid FROM db_tevent WHERE ts = ? AND event = ?`, eventTs.UnixMilli(), ActivityEventName)
if uuidStr == "" {
return nil
}
// we're going to update this app:activity event back to nowTs
tsLocal := utilfn.ConvertToWallClockPT(nowTs).Format(time.RFC3339)
query := `UPDATE db_tevent SET ts = ?, tslocal = ? WHERE uuid = ?`
tx.Exec(query, nowTs.UnixMilli(), tsLocal, uuidStr)
return nil
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Potential Day Boundary Issue in Telemetry Truncation Logic

  • The usage references confirm that TruncateActivityTEventForShutdown is invoked during server shutdown.
  • The function computes the target timestamp by truncating the current time to the hour and then adding one hour. This means that when shutdown occurs close to midnight (e.g., at 23:59), the computed timestamp (eventTs) will fall on the next day (00:00), which could lead to a mismatch when trying to locate the corresponding "app:activity" event in the database.
  • There is no additional logic in the function to handle day boundaries explicitly. This behavior may be intentional, or it might be an oversight that could lead to corner cases near midnight.
🔗 Analysis chain

Potential edge case with truncation.
Shifting from the next rounded hour back to the current time for shutdown events might create corner cases near day boundaries.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Look for usage references to TruncateActivityTEventForShutdown, confirm day boundary handling
rg -A 5 "TruncateActivityTEventForShutdown"

Length of output: 1037

Comment on lines +967 to +988
func ConvertUUIDv4Tov7(uuidv4 string) (string, error) {
// Parse the UUIDv4
parts := strings.Split(uuidv4, "-")
if len(parts) != 5 {
return "", fmt.Errorf("invalid UUIDv4 format")
}

// Section 1 and 2: Fixed timestamp for Jan 1, 2024
section1 := "01823a80" // High 32 bits of the timestamp
section2 := "0000" // Middle 16 bits of the timestamp

// Section 3: Version (7) and the last 3 bytes of randomness from UUIDv4
section3 := "7" + parts[2][1:] // Replace the first nibble with '7' for version

// Section 4 and 5: Copy from the original UUIDv4
section4 := parts[3]
section5 := parts[4]

// Combine sections to form UUIDv7
uuidv7 := fmt.Sprintf("%s-%s-%s-%s-%s", section1, section2, section3, section4, section5)
return uuidv7, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fixed timestamp in UUIDv7 conversion may cause issues.

The function uses a fixed timestamp (Jan 1, 2024) for UUIDv7 conversion, which:

  1. Makes all converted UUIDs appear to be created at the same time
  2. May cause ordering/sorting issues when mixed with real UUIDv7s
  3. Doesn't follow the UUIDv7 specification which requires using the current timestamp

Consider using the current timestamp instead:

-	// Section 1 and 2: Fixed timestamp for Jan 1, 2024
-	section1 := "01823a80" // High 32 bits of the timestamp
-	section2 := "0000"     // Middle 16 bits of the timestamp
+	// Get current Unix timestamp in milliseconds
+	now := time.Now().UnixMilli()
+	// Extract timestamp bits
+	section1 := fmt.Sprintf("%08x", now>>16)  // High 32 bits
+	section2 := fmt.Sprintf("%04x", now&0xFFFF)  // Low 16 bits
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func ConvertUUIDv4Tov7(uuidv4 string) (string, error) {
// Parse the UUIDv4
parts := strings.Split(uuidv4, "-")
if len(parts) != 5 {
return "", fmt.Errorf("invalid UUIDv4 format")
}
// Section 1 and 2: Fixed timestamp for Jan 1, 2024
section1 := "01823a80" // High 32 bits of the timestamp
section2 := "0000" // Middle 16 bits of the timestamp
// Section 3: Version (7) and the last 3 bytes of randomness from UUIDv4
section3 := "7" + parts[2][1:] // Replace the first nibble with '7' for version
// Section 4 and 5: Copy from the original UUIDv4
section4 := parts[3]
section5 := parts[4]
// Combine sections to form UUIDv7
uuidv7 := fmt.Sprintf("%s-%s-%s-%s-%s", section1, section2, section3, section4, section5)
return uuidv7, nil
}
func ConvertUUIDv4Tov7(uuidv4 string) (string, error) {
// Parse the UUIDv4
parts := strings.Split(uuidv4, "-")
if len(parts) != 5 {
return "", fmt.Errorf("invalid UUIDv4 format")
}
// Get current Unix timestamp in milliseconds
now := time.Now().UnixMilli()
// Extract timestamp bits
section1 := fmt.Sprintf("%08x", now>>16) // High 32 bits
section2 := fmt.Sprintf("%04x", now&0xFFFF) // Low 16 bits
// Section 3: Version (7) and the last 3 bytes of randomness from UUIDv4
section3 := "7" + parts[2][1:] // Replace the first nibble with '7' for version
// Section 4 and 5: Copy from the original UUIDv4
section4 := parts[3]
section5 := parts[4]
// Combine sections to form UUIDv7
uuidv7 := fmt.Sprintf("%s-%s-%s-%s-%s", section1, section2, section3, section4, section5)
return uuidv7, nil
}

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (4)
pkg/telemetry/telemetrydata/telemetrydata.go (2)

43-54: Consider adding validation for user property values.

The TEventUserProps and TEventProps structs provide comprehensive telemetry data points. However, consider adding validation for property values to prevent invalid or malformed data.

For example:

 func (te *TEvent) Validate(current bool) error {
     // ... existing validation ...
+    if te.Props.ClientVersion != "" {
+        if !semver.IsValid(te.Props.ClientVersion) {
+            return fmt.Errorf("invalid client version: %q", te.Props.ClientVersion)
+        }
+    }
+    if te.Props.ClientArch != "" {
+        validArchs := map[string]bool{"amd64": true, "arm64": true}
+        if !validArchs[te.Props.ClientArch] {
+            return fmt.Errorf("invalid client arch: %q", te.Props.ClientArch)
+        }
+    }
     return nil
 }

Also applies to: 56-72


126-181: Consider adding rate limiting for event validation.

The validation logic is thorough but could benefit from rate limiting to prevent event flooding.

For example:

+var eventRateLimiter = make(map[string]time.Time)
+const minEventInterval = 100 * time.Millisecond

 func (te *TEvent) Validate(current bool) error {
     // ... existing validation ...
+    if current {
+        lastEventTime, exists := eventRateLimiter[te.Event]
+        now := time.Now()
+        if exists && now.Sub(lastEventTime) < minEventInterval {
+            return fmt.Errorf("event %q is being sent too frequently", te.Event)
+        }
+        eventRateLimiter[te.Event] = now
+    }
     return nil
 }
frontend/app/block/blockframe.tsx (1)

186-186: Consider recording both magnify and minimize actions.

Currently, the telemetry event is only recorded when a block is magnified. Consider recording both magnify and minimize actions to get a complete picture of block size toggling behavior.

-recordTEvent("action:magnify");
+recordTEvent("action:magnify", { magnified });
frontend/types/gotypes.d.ts (1)

772-784: LGTM! Consider adding JSDoc comments for better documentation.

The TEventUserProps type effectively captures user-specific properties. All fields are appropriately optional, and the location codes follow ISO standards.

Consider adding JSDoc comments to document the expected format of key fields:

 // telemetrydata.TEventUserProps
+/**
+ * User properties for telemetry events
+ * @property {string} client:arch - The architecture of the client (e.g., 'x64', 'arm64')
+ * @property {string} loc:countrycode - ISO 3166-1 alpha-2 country code
+ * @property {string} loc:regioncode - ISO 3166-2 region code
+ */
 type TEventUserProps = {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7b0769f and 181ca56.

📒 Files selected for processing (11)
  • cmd/server/main-server.go (6 hunks)
  • frontend/app/block/blockframe.tsx (2 hunks)
  • frontend/app/store/global.ts (3 hunks)
  • frontend/app/tab/tab.tsx (2 hunks)
  • frontend/types/gotypes.d.ts (1 hunks)
  • pkg/panichandler/panichandler.go (2 hunks)
  • pkg/telemetry/telemetrydata/telemetrydata.go (1 hunks)
  • pkg/waveai/waveai.go (2 hunks)
  • pkg/wcore/block.go (2 hunks)
  • pkg/wcore/workspace.go (2 hunks)
  • pkg/wshrpc/wshserver/wshserver.go (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • pkg/panichandler/panichandler.go
  • pkg/wshrpc/wshserver/wshserver.go
🔇 Additional comments (20)
pkg/waveai/waveai.go (3)

11-11: LGTM!

The import of the telemetrydata package is correctly added for the new telemetry event recording.


67-86: LGTM!

The backendType variable is correctly assigned based on the API type, providing valuable telemetry data about which AI backend is being used.


91-96: LGTM!

The telemetry event is correctly recorded with:

  • Valid event name: "action:runaicmd"
  • Relevant property: AiBackendType
pkg/telemetry/telemetrydata/telemetrydata.go (4)

16-27: LGTM!

The ValidEventNames map provides a clear whitelist of allowed event names, ensuring consistency and preventing arbitrary events.


29-41: LGTM!

The TEvent struct is well-designed with:

  • UUID for unique identification
  • Both Unix and local timestamps
  • Clear separation of DB fields

74-95: LGTM!

The event creation functions are well-implemented with:

  • UUID generation for uniqueness
  • Proper error handling for invalid events
  • Type-safe property handling

97-124: LGTM!

The timestamp and JSON conversion methods are well-implemented with:

  • Proper timezone handling
  • Lazy initialization of timestamps
  • Clear error handling for JSON conversion
pkg/wcore/block.go (2)

17-17: LGTM!

The import of the telemetrydata package is correctly added for the new telemetry event recording.


110-115: LGTM!

The telemetry event recording is well-implemented with:

  • Non-blocking execution in a goroutine
  • Proper panic handling
  • Relevant block view property
frontend/app/tab/tab.tsx (2)

4-4: LGTM!

The recordTEvent function is correctly imported from the global store.


186-187: LGTM!

The activity command and telemetry event recording are well-implemented:

  • ActivityCommand is correctly modified to suppress response
  • Telemetry event is recorded with the correct event name
cmd/server/main-server.go (5)

25-25: LGTM!

The import of the telemetrydata package is required for the new telemetry event recording functionality.


86-91: LGTM!

The function rename from configWatcher to startConfigWatcher improves clarity by better indicating its purpose.


105-114: LGTM!

The function now records a telemetry event with panic details, which will help in tracking and debugging application panics.


157-176: LGTM!

The function now records a comprehensive telemetry event for application startup, including:

  • Client version and build information
  • Architecture and OS details
  • Auto-update configuration
  • Development mode status

This will provide valuable insights into the application's deployment and configuration landscape.


187-195: LGTM!

The function now properly handles telemetry data during shutdown by:

  1. Truncating activity telemetry data
  2. Recording a shutdown event

This ensures clean telemetry data management during application termination.

pkg/wcore/workspace.go (1)

240-242: LGTM!

The function now records a granular telemetry event for tab creation, complementing the existing activity update.

frontend/app/store/global.ts (1)

672-677: LGTM!

The function provides a clean interface for recording telemetry events with:

  • Optional properties with sensible defaults
  • Fire-and-forget behavior using noresponse option
frontend/types/gotypes.d.ts (2)

748-755: LGTM! Well-structured telemetry event type.

The TEvent type has a clean design with appropriate optional and required fields, ensuring essential telemetry data is captured while allowing flexibility in timestamp and identifier generation.


757-770: LGTM! Comprehensive telemetry properties type.

The TEventProps type effectively captures various metrics and properties:

  • Activity metrics are properly optional
  • Action initiator uses a strict union type
  • Follows telemetry best practices with $set and $set_once properties

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
pkg/remote/conncontroller/conncontroller.go (2)

560-565: LGTM! Consider enriching error events.

The error event recording is well-placed and correctly implemented. Consider including additional context in error events, such as the error message or error type, to help with debugging and analytics.

 telemetry.GoRecordTEventWrap(&telemetrydata.TEvent{
   Event: "conn:connecterror",
   Props: telemetrydata.TEventProps{
     ConnType: "ssh",
+    Error:    err.Error(),
   },
 })

576-581: LGTM! Consider using constants for event names.

The success event recording is well-placed and correctly implemented. Consider defining constants for event names to ensure consistency and prevent typos.

const (
    EventConnectError = "conn:connecterror"
    EventConnect      = "conn:connect"
)

Then use these constants in the event recording:

 telemetry.GoRecordTEventWrap(&telemetrydata.TEvent{
-  Event: "conn:connect",
+  Event: EventConnect,
   Props: telemetrydata.TEventProps{
     ConnType: "ssh",
   },
 })
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between be31352 and 1572414.

📒 Files selected for processing (3)
  • pkg/remote/conncontroller/conncontroller.go (3 hunks)
  • pkg/telemetry/telemetrydata/telemetrydata.go (1 hunks)
  • pkg/wslconn/wslconn.go (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/telemetry/telemetrydata/telemetrydata.go
🔇 Additional comments (4)
pkg/wslconn/wslconn.go (3)

23-23: LGTM!

The telemetrydata package import is correctly added and necessary for the new telemetry event tracking functionality.


538-543: LGTM!

The telemetry event for connection errors is well-placed and includes appropriate properties.


554-559: LGTM!

The telemetry event for successful connections is well-placed and includes appropriate properties.

pkg/remote/conncontroller/conncontroller.go (1)

28-28: LGTM!

The telemetrydata package import is correctly added and follows the existing import grouping pattern.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (4)
emain/emain.ts (3)

462-485: Consider enhancing error handling and validation.

The telemetry event implementation could be improved in the following areas:

  1. Add type checking for display properties before sending.
  2. Use more specific error types for better error tracking.
  3. Consider adding retry logic for failed telemetry events.
 async function sendDisplaysTDataEvent() {
     const displays = getActivityDisplays();
     if (displays.length === 0) {
         return;
     }
+    // Validate display properties
+    if (!displays.every(d => d.height > 0 && d.width > 0 && d.dpr > 0)) {
+        console.log("Invalid display metrics detected");
+        return;
+    }
     const props: TEventProps = {};
     props["display:count"] = displays.length;
     props["display:height"] = displays[0].height;
     props["display:width"] = displays[0].width;
     props["display:dpr"] = displays[0].dpr;
     props["display:all"] = displays;
     try {
         await RpcApi.RecordTEventCommand(
             ElectronWshClient,
             {
                 event: "app:display",
                 props,
             },
             { noresponse: true }
         );
     } catch (e) {
-        console.log("error sending display tdata event", e);
+        if (e instanceof NetworkError) {
+            console.log("Network error sending display tdata event:", e.message);
+        } else {
+            console.log("Unexpected error sending display tdata event:", e);
+        }
     }
 }

500-511: Consider consolidating error handling for telemetry events.

The error handling for both ActivityCommand and RecordTEventCommand could be consolidated to avoid duplication and ensure consistent error handling across telemetry events.

 try {
     await RpcApi.ActivityCommand(ElectronWshClient, activity, { noresponse: true });
     await RpcApi.RecordTEventCommand(
         ElectronWshClient,
         {
             event: "app:activity",
             props: {
                 "activity:activeminutes": activity.activeminutes,
                 "activity:fgminutes": activity.fgminutes,
                 "activity:openminutes": activity.openminutes,
             },
         },
         { noresponse: true }
     );
 } catch (e) {
-    console.log("error logging active state", e);
+    const errorContext = e.message?.includes("ActivityCommand") ? "activity state" : "telemetry event";
+    console.log(`Error logging ${errorContext}:`, e);
 }

660-661: Document the rationale for initialization delays.

Both telemetry functions are initialized with a 5-second delay, but the reasoning isn't documented. Consider adding comments explaining why this specific delay was chosen.

-    setTimeout(runActiveTimer, 5000); // start active timer, wait 5s just to be safe
-    setTimeout(sendDisplaysTDataEvent, 5000);
+    // Wait 5 seconds before starting telemetry to ensure:
+    // 1. All windows and displays are properly initialized
+    // 2. The RPC client is ready to handle requests
+    // 3. Avoid overwhelming the system during startup
+    const TELEMETRY_INIT_DELAY = 5000;
+    setTimeout(runActiveTimer, TELEMETRY_INIT_DELAY);
+    setTimeout(sendDisplaysTDataEvent, TELEMETRY_INIT_DELAY);
pkg/wcloud/wcloud.go (1)

235-267: Improve logging and error messages.

The function could benefit from more descriptive logging and error messages.

Apply this diff to improve the logging and error messages:

 func sendTelemetry(ctx context.Context, clientId string) error {
 	activity, err := telemetry.GetNonUploadedActivity(ctx)
 	if err != nil {
-		return fmt.Errorf("cannot get activity: %v", err)
+		return fmt.Errorf("failed to retrieve non-uploaded activity data: %v", err)
 	}
 	if len(activity) == 0 {
+		log.Printf("[wcloud] no telemetry activity to send\n")
 		return nil
 	}
-	log.Printf("[wcloud] sending telemetry data\n")
+	log.Printf("[wcloud] sending telemetry data with %d activities\n", len(activity))
 	dayStr := daystr.GetCurDayStr()
 	input := TelemetryInputType{
 		ClientId:          clientId,
 		UserId:            clientId,
 		AppType:           "w2",
 		AutoUpdateEnabled: telemetry.IsAutoUpdateEnabled(),
 		AutoUpdateChannel: telemetry.AutoUpdateChannel(),
 		CurDay:            dayStr,
 		Activity:          activity,
 	}
 	req, err := makeAnonPostReq(ctx, TelemetryUrl, input)
 	if err != nil {
 		return err
 	}
 	_, err = doRequest(req, nil)
 	if err != nil {
 		return err
 	}
 	err = telemetry.MarkActivityAsUploaded(ctx, activity)
 	if err != nil {
-		return fmt.Errorf("error marking activity as uploaded: %v", err)
+		return fmt.Errorf("failed to mark telemetry activity as uploaded: %v", err)
 	}
+	log.Printf("[wcloud] successfully sent telemetry data\n")
 	return nil
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1572414 and af9d628.

📒 Files selected for processing (4)
  • emain/emain.ts (3 hunks)
  • frontend/types/gotypes.d.ts (1 hunks)
  • pkg/telemetry/telemetrydata/telemetrydata.go (1 hunks)
  • pkg/wcloud/wcloud.go (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/telemetry/telemetrydata/telemetrydata.go
🔇 Additional comments (4)
pkg/wcloud/wcloud.go (1)

153-156: LGTM!

The struct is well-designed with proper json tags and appropriate field types.

frontend/types/gotypes.d.ts (3)

748-755: LGTM!

The type is well-designed with clear field names and proper optionality. The required fields (event and props) ensure that essential data is always present.


757-776: LGTM!

The type is well-organized with clear field names and proper types. The properties are logically grouped and cover various aspects of telemetry data collection.


778-790: LGTM!

The type is well-designed with clear field names and proper optionality. The properties effectively capture user-specific data while maintaining privacy by not collecting PII.

Comment on lines +192 to +212
func sendTEvents(clientId string) (int, error) {
numIters := 0
totalEvents := 0
for {
numIters++
done, numEvents, err := sendTEventsBatch(clientId)
if err != nil {
log.Printf("error sending telemetry events: %v\n", err)
break
}
totalEvents += numEvents
if done {
break
}
if numIters > TEventsMaxBatches {
log.Printf("sendTEvents, hit %d iterations, stopping\n", numIters)
break
}
}
return totalEvents, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling and logging.

The function has several issues:

  1. Errors from sendTEventsBatch are logged but not propagated, which could hide important errors.
  2. The function continues to process remaining batches even after an error.
  3. The log message for hitting max iterations could be more descriptive.

Apply this diff to fix the issues:

 func sendTEvents(clientId string) (int, error) {
 	numIters := 0
 	totalEvents := 0
+	var lastError error
 	for {
 		numIters++
 		done, numEvents, err := sendTEventsBatch(clientId)
 		if err != nil {
 			log.Printf("error sending telemetry events: %v\n", err)
+			lastError = err
 			break
 		}
 		totalEvents += numEvents
 		if done {
 			break
 		}
 		if numIters > TEventsMaxBatches {
-			log.Printf("sendTEvents, hit %d iterations, stopping\n", numIters)
+			log.Printf("[wcloud] reached maximum number of batches (%d), remaining events will be sent in the next iteration\n", TEventsMaxBatches)
 			break
 		}
 	}
-	return totalEvents, nil
+	return totalEvents, lastError
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func sendTEvents(clientId string) (int, error) {
numIters := 0
totalEvents := 0
for {
numIters++
done, numEvents, err := sendTEventsBatch(clientId)
if err != nil {
log.Printf("error sending telemetry events: %v\n", err)
break
}
totalEvents += numEvents
if done {
break
}
if numIters > TEventsMaxBatches {
log.Printf("sendTEvents, hit %d iterations, stopping\n", numIters)
break
}
}
return totalEvents, nil
}
func sendTEvents(clientId string) (int, error) {
numIters := 0
totalEvents := 0
var lastError error
for {
numIters++
done, numEvents, err := sendTEventsBatch(clientId)
if err != nil {
log.Printf("error sending telemetry events: %v\n", err)
lastError = err
break
}
totalEvents += numEvents
if done {
break
}
if numIters > TEventsMaxBatches {
log.Printf("[wcloud] reached maximum number of batches (%d), remaining events will be sent in the next iteration\n", TEventsMaxBatches)
break
}
}
return totalEvents, lastError
}

Comment on lines +162 to +190
func sendTEventsBatch(clientId string) (bool, int, error) {
ctx, cancelFn := context.WithTimeout(context.Background(), WCloudDefaultTimeout)
defer cancelFn()
events, err := telemetry.GetNonUploadedTEvents(ctx, TEventsBatchSize)
if err != nil {
return true, 0, fmt.Errorf("cannot get events: %v", err)
}
if len(events) == 0 {
return true, 0, nil
}
log.Printf("[wcloud] sending %d tevents\n", len(events))
input := TEventsInputType{
ClientId: clientId,
Events: events,
}
req, err := makeAnonPostReq(ctx, TEventsUrl, input)
if err != nil {
return true, 0, err
}
_, err = doRequest(req, nil)
if err != nil {
return true, 0, err
}
err = telemetry.MarkTEventsAsUploaded(ctx, events)
if err != nil {
return true, 0, fmt.Errorf("error marking activity as uploaded: %v", err)
}
return len(events) < TEventsBatchSize, len(events), nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix error handling and error messages.

The function has several issues:

  1. When an error occurs, the function returns true for the done parameter, which could cause the caller to stop prematurely even if there are more events to process.
  2. Error messages could be more descriptive and consistent.

Apply this diff to fix the issues:

 func sendTEventsBatch(clientId string) (bool, int, error) {
 	ctx, cancelFn := context.WithTimeout(context.Background(), WCloudDefaultTimeout)
 	defer cancelFn()
 	events, err := telemetry.GetNonUploadedTEvents(ctx, TEventsBatchSize)
 	if err != nil {
-		return true, 0, fmt.Errorf("cannot get events: %v", err)
+		return false, 0, fmt.Errorf("failed to retrieve non-uploaded telemetry events: %v", err)
 	}
 	if len(events) == 0 {
 		return true, 0, nil
 	}
 	log.Printf("[wcloud] sending %d tevents\n", len(events))
 	input := TEventsInputType{
 		ClientId: clientId,
 		Events:   events,
 	}
 	req, err := makeAnonPostReq(ctx, TEventsUrl, input)
 	if err != nil {
-		return true, 0, err
+		return false, 0, err
 	}
 	_, err = doRequest(req, nil)
 	if err != nil {
-		return true, 0, err
+		return false, 0, err
 	}
 	err = telemetry.MarkTEventsAsUploaded(ctx, events)
 	if err != nil {
-		return true, 0, fmt.Errorf("error marking activity as uploaded: %v", err)
+		return false, 0, fmt.Errorf("error marking telemetry events as uploaded: %v", err)
 	}
 	return len(events) < TEventsBatchSize, len(events), nil
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func sendTEventsBatch(clientId string) (bool, int, error) {
ctx, cancelFn := context.WithTimeout(context.Background(), WCloudDefaultTimeout)
defer cancelFn()
events, err := telemetry.GetNonUploadedTEvents(ctx, TEventsBatchSize)
if err != nil {
return true, 0, fmt.Errorf("cannot get events: %v", err)
}
if len(events) == 0 {
return true, 0, nil
}
log.Printf("[wcloud] sending %d tevents\n", len(events))
input := TEventsInputType{
ClientId: clientId,
Events: events,
}
req, err := makeAnonPostReq(ctx, TEventsUrl, input)
if err != nil {
return true, 0, err
}
_, err = doRequest(req, nil)
if err != nil {
return true, 0, err
}
err = telemetry.MarkTEventsAsUploaded(ctx, events)
if err != nil {
return true, 0, fmt.Errorf("error marking activity as uploaded: %v", err)
}
return len(events) < TEventsBatchSize, len(events), nil
}
func sendTEventsBatch(clientId string) (bool, int, error) {
ctx, cancelFn := context.WithTimeout(context.Background(), WCloudDefaultTimeout)
defer cancelFn()
events, err := telemetry.GetNonUploadedTEvents(ctx, TEventsBatchSize)
if err != nil {
return false, 0, fmt.Errorf("failed to retrieve non-uploaded telemetry events: %v", err)
}
if len(events) == 0 {
return true, 0, nil
}
log.Printf("[wcloud] sending %d tevents\n", len(events))
input := TEventsInputType{
ClientId: clientId,
Events: events,
}
req, err := makeAnonPostReq(ctx, TEventsUrl, input)
if err != nil {
return false, 0, err
}
_, err = doRequest(req, nil)
if err != nil {
return false, 0, err
}
err = telemetry.MarkTEventsAsUploaded(ctx, events)
if err != nil {
return false, 0, fmt.Errorf("error marking telemetry events as uploaded: %v", err)
}
return len(events) < TEventsBatchSize, len(events), nil
}

Comment on lines +214 to +233
func SendAllTelemetry(ctx context.Context, clientId string) error {
defer func() {
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
defer cancelFn()
telemetry.CleanOldTEvents(ctx)
}()
if !telemetry.IsTelemetryEnabled() {
log.Printf("telemetry disabled, not sending\n")
return nil
}
_, err := sendTEvents(clientId)
if err != nil {
return err
}
err = sendTelemetry(ctx, clientId)
if err != nil {
return err
}
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve context handling and error messages.

The function has several issues:

  1. The cleanup operation uses a fixed 2-second timeout, which could be too short for large datasets.
  2. The function ignores the context passed as a parameter.
  3. Error handling could be more descriptive.

Apply this diff to fix the issues:

 func SendAllTelemetry(ctx context.Context, clientId string) error {
 	defer func() {
-		ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
+		cleanupCtx, cancelFn := context.WithTimeout(context.Background(), 30*time.Second)
 		defer cancelFn()
-		telemetry.CleanOldTEvents(ctx)
+		if err := telemetry.CleanOldTEvents(cleanupCtx); err != nil {
+			log.Printf("[wcloud] error cleaning old telemetry events: %v\n", err)
+		}
 	}()
 	if !telemetry.IsTelemetryEnabled() {
 		log.Printf("telemetry disabled, not sending\n")
 		return nil
 	}
 	_, err := sendTEvents(clientId)
 	if err != nil {
-		return err
+		return fmt.Errorf("failed to send telemetry events: %w", err)
 	}
 	err = sendTelemetry(ctx, clientId)
 	if err != nil {
-		return err
+		return fmt.Errorf("failed to send telemetry data: %w", err)
 	}
 	return nil
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func SendAllTelemetry(ctx context.Context, clientId string) error {
defer func() {
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
defer cancelFn()
telemetry.CleanOldTEvents(ctx)
}()
if !telemetry.IsTelemetryEnabled() {
log.Printf("telemetry disabled, not sending\n")
return nil
}
_, err := sendTEvents(clientId)
if err != nil {
return err
}
err = sendTelemetry(ctx, clientId)
if err != nil {
return err
}
return nil
}
func SendAllTelemetry(ctx context.Context, clientId string) error {
defer func() {
cleanupCtx, cancelFn := context.WithTimeout(context.Background(), 30*time.Second)
defer cancelFn()
if err := telemetry.CleanOldTEvents(cleanupCtx); err != nil {
log.Printf("[wcloud] error cleaning old telemetry events: %v\n", err)
}
}()
if !telemetry.IsTelemetryEnabled() {
log.Printf("telemetry disabled, not sending\n")
return nil
}
_, err := sendTEvents(clientId)
if err != nil {
return fmt.Errorf("failed to send telemetry events: %w", err)
}
err = sendTelemetry(ctx, clientId)
if err != nil {
return fmt.Errorf("failed to send telemetry data: %w", err)
}
return nil
}

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (5)
frontend/types/gotypes.d.ts (2)

757-783: Consider adding JSDoc comments for complex properties.

The TEventProps type has a comprehensive set of properties with clear naming conventions. However, some properties could benefit from documentation:

  • $set and $set_once behavior and differences
  • Complex types like display:all

Example documentation:

 // telemetrydata.TEventProps
+/**
+ * Properties for telemetry events
+ * @property {TEventUserProps} $set - Properties to set on the user profile
+ * @property {TEventUserProps} $set_once - Properties to set only if not already set
+ * @property {any} display:all - Complete display information
+ */
 type TEventProps = {

785-797: Consider adding validation for country and region codes.

The TEventUserProps type includes location properties but doesn't enforce standard formats:

  • loc:countrycode should be ISO 3166-1 alpha-2
  • loc:regioncode should be ISO 3166-2

Consider using string literals to enforce standard formats:

-        "loc:countrycode"?: string;
-        "loc:regioncode"?: string;
+        "loc:countrycode"?: `${Uppercase<string>}${Uppercase<string>}`;
+        "loc:regioncode"?: `${Uppercase<string>}${Uppercase<string>}-${Uppercase<string>}${Uppercase<string>}`;
pkg/util/utilfn/compare.go (2)

12-12: Add function documentation.

Please add a function comment explaining the purpose, behavior, and potential limitations of this function. This helps other developers understand when to use it appropriately.

Add this documentation:

+// CompareAsMarshaledJson compares two values by marshaling them to JSON and comparing the results.
+// Returns true if both values marshal to identical JSON representations.
+// Note: This comparison is sensitive to field ordering in structs and can be computationally
+// expensive for large objects. Consider using direct equality or JsonValEqual for simple comparisons.
 func CompareAsMarshaledJson(a, b any) bool {

12-28: Consider performance and JSON field order implications.

While the implementation is correct, there are two important considerations:

  1. JSON marshaling is computationally expensive compared to direct equality checks or JsonValEqual. Consider using this only when deep comparison of complex structures is necessary.

  2. The comparison is sensitive to field ordering in JSON objects. Two equivalent structs with fields defined in different orders will be considered unequal.

Consider adding a comment in the PR description about these limitations and when to use this function versus alternatives like JsonValEqual or direct equality.

cmd/server/main-server.go (1)

159-173: Consider adding a cancellation mechanism.

While the telemetry counts loop is well-implemented, consider adding a cancellation mechanism to allow for graceful shutdown.

-func updateTelemetryCountsLoop() {
+func updateTelemetryCountsLoop(ctx context.Context) {
 	defer func() {
 		panichandler.PanicHandler("updateTelemetryCountsLoop", recover())
 	}()
 	var nextSend int64
 	var lastCounts telemetrydata.TEventProps
 	time.Sleep(TelemetryInitialCountsWait)
 	for {
+		select {
+		case <-ctx.Done():
+			return
+		default:
 			if time.Now().Unix() > nextSend {
 				nextSend = time.Now().Add(TelemetryCountsInterval).Unix()
 				lastCounts = updateTelemetryCounts(lastCounts)
 			}
 			time.Sleep(TelemetryTick)
+		}
 	}
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between af9d628 and fbb2e77.

📒 Files selected for processing (4)
  • cmd/server/main-server.go (7 hunks)
  • frontend/types/gotypes.d.ts (1 hunks)
  • pkg/telemetry/telemetrydata/telemetrydata.go (1 hunks)
  • pkg/util/utilfn/compare.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/telemetry/telemetrydata/telemetrydata.go
🔇 Additional comments (10)
frontend/types/gotypes.d.ts (1)

748-755: LGTM! Well-structured event type definition.

The TEvent type is well-designed with:

  • Clear separation between metadata (uuid, timestamps) and event data
  • Required fields (event, props) for essential data
  • Optional fields for flexibility
pkg/util/utilfn/compare.go (1)

7-8: LGTM!

The new imports are appropriate for the JSON marshaling and comparison functionality being added.

cmd/server/main-server.go (8)

25-25: LGTM! Well-defined telemetry timing constants.

The new telemetry timing constants are well-structured with reasonable intervals (5s initial wait, 1h interval for counts).

Also applies to: 51-52


89-94: LGTM! Improved function naming.

The rename from configWatcher to startConfigWatcher better reflects the function's purpose as it initiates the configuration watching process.


137-157: LGTM! Well-structured telemetry counts collection.

The function effectively collects various system metrics with proper error handling and duplicate prevention through JSON comparison.


108-117: LGTM! Enhanced panic telemetry.

The panic handler now includes more detailed information through the new tevents system.


190-218: LGTM! Comprehensive startup telemetry.

The startup telemetry now includes detailed system information and proper error handling. The initialization order is correct with startupActivityUpdate called after startConfigWatcher.


220-237: LGTM! Enhanced shutdown telemetry.

The shutdown process now properly records shutdown events and truncates activity data.


362-366: LGTM! Proper initialization order.

The startup sequence is well-ordered:

  1. Configuration watcher
  2. Telemetry loops
  3. Startup activity update

119-135: Verify if 10s timeout is sufficient for all scenarios.

The timeout has been increased from 5s to 10s. Please verify this is sufficient for high-load scenarios.

✅ Verification successful

Telemetry Timeout Verification: 10s Timeout is Sufficient

After reviewing the telemetry-related code and searching for any "timeout" or "deadline exceeded" error messages across the codebase, no telemetry-specific timeout errors have been observed. The telemetry function in question now uses a 10-second timeout, and all related operations (from database calls to sending telemetry) consistently use appropriate timeout values.

  • The search output did not yield any error logs indicating that the telemetry sending operation is timing out.
  • Other components using similar timeout patterns remain stable under load.
  • No evidence suggests that high-load scenarios have resulted in telemetry timeout issues.

Based on the current code and log analysis, the increased 10s timeout is sufficient.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any telemetry-related timeout issues in logs
# Test: Search for timeout errors in telemetry sending
rg -i "timeout|deadline exceeded" -A 3 -B 3

Length of output: 107658

@sawka sawka merged commit d7a9006 into main Feb 3, 2025
9 of 10 checks passed
@sawka sawka deleted the sawka/analytics2 branch February 3, 2025 23:32
xxyy2024 pushed a commit to xxyy2024/waveterm_aipy that referenced this pull request Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant